<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of color</title>
  <meta name="keywords" content="color">
  <meta name="description" content="color(g,algo) --- color the graph g by a given algorithm">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2003 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../../m2html.css">
</head>
<body>
<a name="_top"></a>
<div><a href="../../index.html">Home</a> &gt;  <a href="../index.html">matgraph</a> &gt; <a href="index.html">@graph</a> &gt; color.m</div>

<!--<table width="100%"><tr><td align="left"><a href="../../index.html"><img alt="<" border="0" src="../../left.png">&nbsp;Master index</a></td>
<td align="right"><a href="index.html">Index for matgraph/@graph&nbsp;<img alt=">" border="0" src="../../right.png"></a></td></tr></table>-->

<h1>color
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>color(g,algo) --- color the graph g by a given algorithm</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>function p = color(g,algo,max_time) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre class="comment"> color(g,algo) --- color the graph g by a given algorithm
 The algorithms are as follows:

 'greedy':     the default, puts vertices in descending order of degree 
               and runs sequential coloring

 'rs':         random sequence, puts vertices in random order and 
               runs sequential coloring

 'repeat':     runs the 'rs' algorithm repeatedly until a fixed amount
               of time passes (30 seconds); this can also be called as
               color(g,'repeat',max_time)

 'optimal':    always finds a minimum coloring (slow on large graphs)

 'matlab':     use the COLOR method from the Optimization Toolbox
               (this is experimental --- it appears to give the same
               result as 'greedy')

 Algorithms rs, repeat, and optimal coded by Brian Towne.</pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="color.html" class="code" title="function p = color(g,algo,max_time)">color</a>	color(g,algo) --- color the graph g by a given algorithm</li><li><a href="deg.html" class="code" title="function d = deg(g,v)">deg</a>	deg: degree of a vertex or degree sequence</li><li><a href="incidence_matrix.html" class="code" title="function M = incidence_matrix(g,type)">incidence_matrix</a>	incidence_matrix(g) --- return the vertex/edge incidence matrix.</li><li><a href="neighbors.html" class="code" title="function nlist = neighbors(g,v)">neighbors</a>	neighbors(g,v) --- neighbors of v as a list.</li><li><a href="nv.html" class="code" title="function n = nv(g)">nv</a>	nv(g) --- number of vertices in g</li></ul>
This function is called by:
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="cdraw.html" class="code" title="function cdraw(g,coloring,line_style,color_matrix)">cdraw</a>	cdraw(g,coloring) -- draw g with a given vertex coloring</li><li><a href="color.html" class="code" title="function p = color(g,algo,max_time)">color</a>	color(g,algo) --- color the graph g by a given algorithm</li><li><a href="edge_color.html" class="code" title="function c = edge_color(g,algo)">edge_color</a>	edge_color(g,algo) --- find a proper edge coloring of the graph g</li></ul>
<!-- crossreference -->

<h2><a name="_subfunctions"></a>SUBFUNCTIONS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<ul style="list-style-image:url(../../matlabicon.gif)">
<li><a href="#_sub1" class="code">function p = greedy_color(g)</a></li><li><a href="#_sub2" class="code">function p = rs_color(g,max_colors)</a></li><li><a href="#_sub3" class="code">function p = repeat(g,max_time)</a></li><li><a href="#_sub4" class="code">function p = optimal_color(g)</a></li><li><a href="#_sub5" class="code">function p = complete_extend(g,v,colors,best)</a></li><li><a href="#_sub6" class="code">function p = sequential_color(g,vlist,max_colors)</a></li><li><a href="#_sub7" class="code">function p = matlab_color(g)</a></li></ul>
<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function p = color(g,algo,max_time)</a>
0002 <span class="comment">% color(g,algo) --- color the graph g by a given algorithm</span>
0003 <span class="comment">% The algorithms are as follows:</span>
0004 <span class="comment">%</span>
0005 <span class="comment">% 'greedy':     the default, puts vertices in descending order of degree</span>
0006 <span class="comment">%               and runs sequential coloring</span>
0007 <span class="comment">%</span>
0008 <span class="comment">% 'rs':         random sequence, puts vertices in random order and</span>
0009 <span class="comment">%               runs sequential coloring</span>
0010 <span class="comment">%</span>
0011 <span class="comment">% 'repeat':     runs the 'rs' algorithm repeatedly until a fixed amount</span>
0012 <span class="comment">%               of time passes (30 seconds); this can also be called as</span>
0013 <span class="comment">%               color(g,'repeat',max_time)</span>
0014 <span class="comment">%</span>
0015 <span class="comment">% 'optimal':    always finds a minimum coloring (slow on large graphs)</span>
0016 <span class="comment">%</span>
0017 <span class="comment">% 'matlab':     use the COLOR method from the Optimization Toolbox</span>
0018 <span class="comment">%               (this is experimental --- it appears to give the same</span>
0019 <span class="comment">%               result as 'greedy')</span>
0020 <span class="comment">%</span>
0021 <span class="comment">% Algorithms rs, repeat, and optimal coded by Brian Towne.</span>
0022 
0023 <span class="keyword">if</span> nargin &lt;= 1
0024     algo = <span class="string">'greedy'</span>;
0025 <span class="keyword">end</span>
0026 
0027 
0028 <span class="keyword">switch</span> lower(algo)
0029     <span class="keyword">case</span> <span class="string">'greedy'</span>
0030         p = <a href="#_sub1" class="code" title="subfunction p = greedy_color(g)">greedy_color</a>(g);
0031     <span class="keyword">case</span> <span class="string">'rs'</span>
0032         p = <a href="#_sub2" class="code" title="subfunction p = rs_color(g,max_colors)">rs_color</a>(g);
0033     <span class="keyword">case</span> <span class="string">'repeat'</span>
0034         <span class="keyword">if</span> nargin &lt;=2
0035             max_time = 30;
0036         <span class="keyword">end</span>
0037         p = <a href="#_sub3" class="code" title="subfunction p = repeat(g,max_time)">repeat</a>(g,max_time);
0038     <span class="keyword">case</span> <span class="string">'optimal'</span>
0039         p = <a href="#_sub4" class="code" title="subfunction p = optimal_color(g)">optimal_color</a>(g);
0040     <span class="keyword">case</span> <span class="string">'matlab'</span>
0041         p = <a href="#_sub7" class="code" title="subfunction p = matlab_color(g)">matlab_color</a>(g);
0042     <span class="keyword">otherwise</span>
0043         error([<span class="string">'Algorithm &quot;'</span>, algo, <span class="string">'&quot; unimplemented'</span>]);
0044 <span class="keyword">end</span>
0045             
0046 
0047 
0048 
0049 
0050 
0051 <a name="_sub1" href="#_subfunctions" class="code">function p = greedy_color(g)</a>
0052 
0053 n = <a href="nv.html" class="code" title="function n = nv(g)">nv</a>(g);
0054 d = <a href="deg.html" class="code" title="function d = deg(g,v)">deg</a>(g);
0055 [dd,vlist] = sort(-d);
0056 clear dd;
0057 p = <a href="#_sub6" class="code" title="subfunction p = sequential_color(g,vlist,max_colors)">sequential_color</a>(g,vlist,n);
0058 <span class="keyword">return</span>
0059 
0060 
0061 
0062 <a name="_sub2" href="#_subfunctions" class="code">function p = rs_color(g,max_colors)</a>
0063 <span class="comment">% Random sequence</span>
0064 
0065 n = <a href="nv.html" class="code" title="function n = nv(g)">nv</a>(g);
0066 <span class="keyword">if</span> nargin &lt;= 1
0067     max_colors = n;
0068 <span class="keyword">end</span>
0069 vlist = randperm(n);
0070 p = <a href="#_sub6" class="code" title="subfunction p = sequential_color(g,vlist,max_colors)">sequential_color</a>(g,vlist,max_colors);
0071 <span class="keyword">return</span>
0072 
0073 
0074 
0075 <a name="_sub3" href="#_subfunctions" class="code">function p = repeat(g,max_time)</a>
0076 <span class="comment">% Starts w/ greedy coloring, then loops through random permutations</span>
0077 <span class="comment">% until it finds an optimal coloring.</span>
0078 
0079 tic;
0080 <span class="comment">% n = nv(g);</span>
0081 p = <a href="#_sub1" class="code" title="subfunction p = greedy_color(g)">greedy_color</a>(g);
0082 max_colors = np(p)-1;
0083 <span class="keyword">while</span> (toc &lt; max_time)
0084     temp = <a href="#_sub2" class="code" title="subfunction p = rs_color(g,max_colors)">rs_color</a>(g,max_colors);
0085     <span class="keyword">if</span> ~ (np(temp)==0) <span class="comment">% if rs_color returned w/o exhausting max_colors</span>
0086         p = temp;
0087         max_colors = np(p)-1;
0088     <span class="keyword">end</span>
0089 <span class="keyword">end</span>
0090 <span class="keyword">return</span>
0091 
0092 
0093 
0094 <a name="_sub4" href="#_subfunctions" class="code">function p = optimal_color(g)</a>
0095 <span class="comment">% Finds a minimum coloring for g. Works by calling complete_extend.</span>
0096 
0097 n = <a href="nv.html" class="code" title="function n = nv(g)">nv</a>(g);
0098 p = <a href="#_sub1" class="code" title="subfunction p = greedy_color(g)">greedy_color</a>(g);
0099 p = <a href="#_sub5" class="code" title="subfunction p = complete_extend(g,v,colors,best)">complete_extend</a>(g,1,zeros(1,n),p);
0100 <span class="keyword">return</span>
0101 
0102 
0103 
0104 <a name="_sub5" href="#_subfunctions" class="code">function p = complete_extend(g,v,colors,best)</a>
0105 <span class="comment">% Recursive function. Loops through every possible color for a given vertex.</span>
0106 <span class="comment">% p is the current coloring. best is the best coloring found thus far.</span>
0107 
0108 n = <a href="nv.html" class="code" title="function n = nv(g)">nv</a>(g);
0109 vneig = <a href="neighbors.html" class="code" title="function nlist = neighbors(g,v)">neighbors</a>(g,v);  <span class="comment">% neighbors of v</span>
0110 cneig = unique(colors(vneig));  <span class="comment">% colors on neighbors</span>
0111 avail = setdiff(1:min(v,np(best)-1),cneig); <span class="comment">% colors available</span>
0112 <span class="keyword">for</span> k=avail
0113     colors(v)=k;
0114     <span class="keyword">if</span> v == n
0115         mc = max(colors);
0116         cpart = cell(1,mc);
0117         <span class="keyword">for</span> k=1:mc
0118             cpart{k} = find(colors==k);
0119         <span class="keyword">end</span>
0120         p = partition(cpart);
0121         <span class="keyword">if</span> np(p) &lt; np(best)
0122             best = p;
0123         <span class="keyword">end</span>
0124     <span class="keyword">else</span>
0125         best = <a href="#_sub5" class="code" title="subfunction p = complete_extend(g,v,colors,best)">complete_extend</a>(g,v+1,colors,best);
0126     <span class="keyword">end</span>
0127 <span class="keyword">end</span>
0128 p = best;
0129 <span class="keyword">return</span>
0130 
0131 
0132 
0133 <a name="_sub6" href="#_subfunctions" class="code">function p = sequential_color(g,vlist,max_colors)</a>
0134 <span class="comment">% Runs sequential coloring on vertices in order of vlist</span>
0135 <span class="comment">% Exits if it uses more than max_colors. Setting max_colors = nv(g)</span>
0136 <span class="comment">% means this will never happen.</span>
0137 
0138 n = <a href="nv.html" class="code" title="function n = nv(g)">nv</a>(g);
0139 colors = zeros(1,n);
0140 
0141 <span class="comment">% scan vertices by order in vlist</span>
0142 <span class="keyword">for</span> k=1:n
0143     v = vlist(k);
0144     vneig = <a href="neighbors.html" class="code" title="function nlist = neighbors(g,v)">neighbors</a>(g,v);  <span class="comment">% neighbors of v</span>
0145     cneig = unique(colors(vneig));  <span class="comment">% colors on neighbors</span>
0146     <span class="keyword">if</span> isempty(cneig) 
0147         mc = 0;
0148     <span class="keyword">else</span>
0149         mc = cneig(length(cneig)); <span class="comment">% max color seen on neighbors</span>
0150     <span class="keyword">end</span>
0151     avail = setdiff(1:mc,cneig); <span class="comment">% colors available (if any)</span>
0152     <span class="keyword">if</span> isempty(avail)
0153         <span class="keyword">if</span> mc+1 &gt; max_colors <span class="comment">% if max_colors is exhausted, exit</span>
0154             p = partition(0);
0155             <span class="keyword">return</span>
0156         <span class="keyword">else</span>
0157             colors(v) = mc+1;
0158         <span class="keyword">end</span>
0159     <span class="keyword">else</span>
0160         colors(v) = min(avail);
0161     <span class="keyword">end</span>
0162 <span class="keyword">end</span>
0163 
0164 mc = max(colors);
0165 cpart = cell(1,mc);
0166 <span class="keyword">for</span> k=1:mc
0167     cpart{k} = find(colors==k);
0168 <span class="keyword">end</span>
0169 p = partition(cpart);
0170 <span class="keyword">return</span>
0171 
0172 
0173 
0174 
0175 <a name="_sub7" href="#_subfunctions" class="code">function p = matlab_color(g)</a>
0176 <span class="comment">% Uses the COLOR method from the optimization toolbox.</span>
0177 <span class="comment">% That COLOR takes as input an interesection graph representation of g. The</span>
0178 <span class="comment">% easiest way for us to do that is to use the incidence matrix, but perhaps</span>
0179 <span class="comment">% if we used a smarter intersection graph representation, we could get</span>
0180 <span class="comment">% better results from the Optimization Toolbox's COLOR.</span>
0181 M = <a href="incidence_matrix.html" class="code" title="function M = incidence_matrix(g,type)">incidence_matrix</a>(g)';
0182 i = <a href="color.html" class="code" title="function p = color(g,algo,max_time)">color</a>(M);
0183 nc = max(i); 
0184 parts = cell(nc,1);
0185 <span class="keyword">for</span> k=1:nc
0186     parts{k} = find(i==k);
0187 <span class="keyword">end</span>
0188 p = partition(parts);
0189 <span class="keyword">return</span>
0190 
0191</pre></div>
<hr><address>Generated on Fri 30-Apr-2010 07:51:16 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> &copy; 2003</address>
</body>
</html>